package com.ylzinfo.jck.service.impl;

import com.ylzinfo.jck.dao.Ac01Mapper;
import com.ylzinfo.jck.domain.Ac01;
import com.ylzinfo.jck.service.Ac01Service;
import com.ylzinfo.jck.util.CkkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author sunqy
 * @create 2019/11/15
 */
@Service("ac01Service")
public class Ac01ServiceImpl implements Ac01Service {
    private Logger logger = LoggerFactory.getLogger(Ac01ServiceImpl.class);

    @Autowired(required = false)
    private Ac01Mapper ac01Mapper;

    @Override
    public Ac01 findAc01ByAac002(String aac002) {
        return ac01Mapper.selectByAac002(aac002);
    }

    /**
     * webservice  根据aac002查询ac01
     * @param aac002
     */
    @Override
    public Ac01 queryByAac002(String aac002){
        return ac01Mapper.selectByAac002(aac002);
    }

    /**
     * webservice插入人员基本信息
     * @param ac01
     */
    @Override
    public int addAc01(Ac01 ac01) {
        return ac01Mapper.insert(ac01);
    }

    /**
     * 根据aac002查询ac01（整合厦门前置库数据）
     * @param aac002
     */
    @Override
    public List<Ac01> listByAac002(String aac002) {
        return ac01Mapper.listByAac002(aac002);
    }

    @Override
    public String init() throws Exception {
        ExecutorService service = Executors.newFixedThreadPool(10);
        List<Ac01> ac01List = new ArrayList<>();
        //本地文件路径
        String filePath = "C:\\Users\\Administrator\\Desktop\\initError.txt";
        BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
        CountDownLatch countDownLatch = new CountDownLatch(ac01List.size());
        for (Ac01 ac01 : ac01List) {
            service.execute(() -> {
                try {
                    //调用持卡库查询接口
                    String aac998 = "";
                    LinkedHashMap<String, String> hashMap = CkkUtil.getPerInfoByJck(ac01);
                    //若aac998为空，调用持卡库新增接口返回aac998本地保存
                    if (hashMap.get("aac998") == null || "".equals(hashMap.get("aac998"))) {
                        aac998 = CkkUtil.addPersonInfoByJck(ac01);
                    } else {
                        aac998 = hashMap.get("aac998");
                    }
                    //若aac998不为空，aac998本地保存
                    Ac01 newAc01 = new Ac01();
                    newAc01.setAac001(ac01.getAac001());
                    newAc01.setAac998(Long.parseLong(aac998));
                    ac01Mapper.updateByPrimaryKeySelective(newAc01);
                }catch (Exception e){
                    //记录错误
                    try {
                        bw.write("人员编号=" + ac01.getAac001() + "," + e.getMessage() + "\r\n");
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        return "初始化完成";
    }
}
